<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>医生排班</title>

    <!-- 引入 Bootstrap 和 Vue -->
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4.5.2/dist/css/bootstrap.min.css">
    <script src="https://cdn.jsdelivr.net/npm/vue@2.6.14/dist/vue.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/axios/dist/axios.min.js"></script>

    <style>
        body {
            background-color: #f4f6f9;
            font-size: 16px;
        }
        .container {
            max-width: 1200px;
            background: #fff;
            padding: 20px;
            border-radius: 8px;
            box-shadow: 0px 0px 10px rgba(0, 0, 0, 0.1);
        }
        h2 {
            font-size: 24px;
            font-weight: bold;
            text-align: center;
            margin-bottom: 20px;
        }
        .table th, .table td {
            text-align: center;
            vertical-align: top;
        }
        .date-box {
            padding: 5px;
            border-radius: 8px;
            background: #f8f9fa;
            height: 100%;
        }
        .section-block {
            border-bottom: 1px solid #e0e0e0;
            padding-bottom: 8px;
            margin-bottom: 8px;
        }
        .section-block:last-child {
            border-bottom: none;
        }
        .form-inline input,
        .form-inline select {
            width: 80px;
            margin: 4px 0;
        }
        .btn-container {
            text-align: center;
            margin-top: 20px;
        }
        .btn {
            font-size: 16px;
            padding: 10px 20px;
        }
    </style>
</head>
<body>
    <div class="container mt-5" id="app">
        <h2>医生排班设置</h2>

        <!-- 医生信息 -->
        <div class="form-group row">
            <label class="col-sm-4 col-form-label font-weight-bold">医生: <?= $goods['goods_name'] ?></label>
            <label class="col-sm-4 col-form-label font-weight-bold">科室: <?= $cat['name'] ?></label>
        </div>

        <!-- 排班表格 -->
        <table class="table table-bordered mt-4">
            <thead>
                <tr>
                    <th v-for="day in daysOfWeek" :key="day">{{ day }}</th>
                </tr>
            </thead>
            <tbody>
                <tr>
                    <td v-for="(day, index) in weeklySchedule" :key="index">
                        <div class="date-box">
                            <div class="section-block" v-for="section in ['morning', 'afternoon', 'night']" :key="section">
                                <label>
                                    <input type="checkbox" v-model="day[section].enabled"> {{ sectionName[section] }}
                                </label>
                                <div v-if="day[section].enabled" class="form-inline">
                                    <select v-model="day[section].mode" class="form-control form-control-sm mx-1">
                                        <option value="fixed">固定</option>
                                        <option value="random">随机</option>
                                    </select>
                                    <template v-if="day[section].mode === 'fixed'">
                                        <input type="number" class="form-control form-control-sm mx-1" v-model.number="day[section].fixed_num" placeholder="数量">
                                    </template>
                                    <template v-else>
                                        <input type="number" class="form-control form-control-sm mx-1" v-model.number="day[section].random_min" placeholder="最小">
                                        <span>~</span>
                                        <input type="number" class="form-control form-control-sm mx-1" v-model.number="day[section].random_max" placeholder="最大">
                                    </template>
                                </div>
                            </div>
                        </div>
                    </td>
                </tr>
            </tbody>
        </table>

        <!-- 按钮 -->
        <div class="btn-container">
            <button class="btn btn-primary" @click="submitSchedule">提交排班</button>
            <button class="btn btn-secondary" @click="goBack">返回</button>
        </div>
    </div>

    <script>
        new Vue({
            el: '#app',
            data() {
                return {
                    goods_id: <?= $goods_id ?>,
                    daysOfWeek: ['周一', '周二', '周三', '周四', '周五', '周六', '周日'],
                    sectionName: {
                        morning: '上午',
                        afternoon: '下午',
                        night: '夜间'
                    },
                    weeklySchedule: []
                };
            },
            mounted() {
                this.initSchedule();
                this.loadSchedule();
            },
            methods: {
                goBack() {
                    window.history.back();
                },
                // 初始化空排班结构
                initSchedule() {
                    this.weeklySchedule = [];
                    for (let i = 1; i <= 7; i++) {
                        this.weeklySchedule.push({
                            week_day: i,
                            morning: { enabled: false, mode: 'fixed', fixed_num: 10, random_min: 5, random_max: 10 },
                            afternoon: { enabled: false, mode: 'fixed', fixed_num: 10, random_min: 5, random_max: 10 },
                            night: { enabled: false, mode: 'fixed', fixed_num: 10, random_min: 5, random_max: 10 }
                        });
                    }
                },
                // 加载排班及号源设置
                loadSchedule() {
                    axios.get(`/store/goods/getWeeklySchedule?goods_id=${this.goods_id}`)
                        .then(response => {
                            const { schedule, source_rules } = response.data;

                            // 设置排班勾选
                            schedule.forEach(item => {
                                const day = this.weeklySchedule.find(d => d.week_day === item.week_day);
                                if (day) {
                                    if (item.morning) day.morning.enabled = true;
                                    if (item.afternoon) day.afternoon.enabled = true;
                                    if (item.night) day.night.enabled = true;
                                }
                            });

                            // 设置号源规则
                            source_rules.forEach(rule => {
                                const day = this.weeklySchedule.find(d => d.week_day === rule.week_day);
                                if (day && day[rule.section]) {
                                    day[rule.section].mode = rule.mode;
                                    day[rule.section].fixed_num = rule.fixed_num;
                                    day[rule.section].random_min = rule.random_min;
                                    day[rule.section].random_max = rule.random_max;
                                }
                            });
                        })
                        .catch(error => {
                            console.error('获取排班数据失败:', error);
                        });
                },
                // 提交排班与号源配置
                submitSchedule() {
                    axios.post(`/store/goods/editWeeklySchedule?goods_id=${this.goods_id}`, {
                        schedule: this.weeklySchedule
                    }).then(response => {
                        alert('排班与号源设置提交成功');
                        window.history.back();
                    }).catch(error => {
                        console.error('提交失败:', error);
                        alert('提交失败');
                    });
                }
            }
        });
    </script>
</body>
</html>